﻿Public Class EditBox : Inherits Windows.Forms.TextBox

	Dim Sheet As Sheet
	Public Twin As EditBox

	Sub New(Parent As Sheet)
		Sheet = Parent
		BorderStyle = Windows.Forms.BorderStyle.None
	End Sub

	Protected Overrides Sub OnLeave(e As System.EventArgs)
		MyBase.OnLeave(e)
		If Twin Is Nothing OrElse Not Twin.Focused Then Sheet.EndEdit()
	End Sub

	Dim NoEvent As Boolean

	Protected Overrides Sub OnTextChanged(e As System.EventArgs)
		MyBase.OnTextChanged(e)
		If NoEvent Then Return
		If TypeOf Parent Is CellEditor And Not Sheet.Editing Then Sheet.BeginEdit(False)
		Twin.SetValue(Text)
	End Sub

	Sub SetValue(Value As String)
		NoEvent = True
		Text = Value
		NoEvent = False
	End Sub

	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
		If keyData = Keys.Enter AndAlso TypeOf Parent Is CellEditor Then
			Twin.Focus()
			SendKeys.Send("{ENTER}")
		End If
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

End Class